﻿Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
    Public Class TokensController
        Friend Function TokensList() As List(Of Token)
            Return TokensList(String.Empty)
        End Function
        Friend Function TokensList(ByVal group As String) As List(Of Token)
            Try
                Dim li As New List(Of Token)
                Dim tk As Token = Nothing
                Dim xDoc As New System.Xml.XmlDocument
                Dim sPath As String = HttpContext.Current.Server.MapPath("~/DesktopModules/activeforums/config/tokens.config")
                xDoc.Load(sPath)
                If Not xDoc Is Nothing Then
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim sQuery As String = "//tokens/token"
                    If Not group = String.Empty Then
                        sQuery = sQuery & "[@group='" & group & "' or @group='*']"
                    End If
                    Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes(sQuery)
                    If xNodeList.Count > 0 Then
                        Dim i As Integer = 0
                        For i = 0 To xNodeList.Count - 1
                            tk = New Token
                            With tk
                                .Group = xNodeList(i).Attributes("group").Value
                                .TokenTag = xNodeList(i).Attributes("name").Value
                                If Not xNodeList(i).Attributes("value") Is Nothing Then
                                    .TokenReplace = Utilities.HTMLDecode(xNodeList(i).Attributes("value").Value)
                                Else
                                    .TokenReplace = Utilities.HTMLDecode(xNodeList(i).ChildNodes.Item(0).InnerText)
                                End If

                            End With
                            li.Add(tk)
                        Next
                    End If
                End If
                Return li
            Catch ex As Exception
                Return Nothing
            End Try

        End Function
        Friend Function TokenGet(ByVal group As String, ByVal TokenName As String) As String
            Dim sOut As String = String.Empty
            Dim tl As List(Of Token) = TokensList(group)
            For Each t As Token In tl
                If t.TokenTag = TokenName Then
                    sOut = t.TokenReplace
                    Exit For
                End If
            Next
            Return sOut
        End Function
    End Class
End Namespace

